
package com.e885.mail;

import java.util.Date;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * Mail Sender<br>
 * 1. 不含袝加檔傳送<br>
 * 
 * @author Grady.Kao
 * @version 1.0
 * @company E885 Group
 * @copyright Copyright (c) 2000-2008
 */

public class MailSender
{
	/**
	 * 郵件主機
	 */
	private String mailServer;

	/**
	 * 寄件人
	 */
	private String from;

	/**
	 * 收件人列表(以 "," 分隔)
	 */
	private String addressList;

	/**
	 * 主旨
	 */
	private String subject;

	/**
	 * 信件內容
	 */
	private String messageText;

	public MailSender( String mailServer, String from )
	{
		this( mailServer, from, null, null, null );
	}

	public MailSender( String mailServer, String from, String addressList, String subject, String messageText )
	{
		this.mailServer = mailServer;
		this.from = from;
		this.addressList = addressList;
		this.subject = subject;
		this.messageText = messageText;
	}

	public void send() throws MessagingException
	{
		Properties props = new Properties();

		props.put( "mail.transport.protocol", "smtp" );
		props.put( "mail.smtp.host", mailServer );
		props.put( "mail.debug", new Boolean( true ) );

		MimeMessage msg = new MimeMessage( Session.getDefaultInstance( props ) );

		msg.setFrom( new InternetAddress( from ) );
		msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse( addressList, true ) );
		msg.setSubject( subject );

		Multipart multipart = new MimeMultipart();

		MimeBodyPart bodyPart = new MimeBodyPart();
		bodyPart.setText( messageText );

		multipart.addBodyPart( bodyPart );

		msg.setContent( multipart );

		msg.setSentDate( new Date() );

		Transport.send( msg );
	}

	/**
	 * @return the addressList
	 */
	public String getAddressList()
	{
		return addressList;
	}

	/**
	 * @param addressList the addressList to set
	 */
	public void setAddressList( String addressList )
	{
		this.addressList = addressList;
	}

	/**
	 * @return the subject
	 */
	public String getSubject()
	{
		return subject;
	}

	/**
	 * @param subject the subject to set
	 */
	public void setSubject( String subject )
	{
		this.subject = subject;
	}

	/**
	 * @return the messageText
	 */
	public String getMessageText()
	{
		return messageText;
	}

	/**
	 * @param messageText the messageText to set
	 */
	public void setMessageText( String messageText )
	{
		this.messageText = messageText;
	}

	/**
	 * @return the mailServer
	 */
	public String getMailServer()
	{
		return mailServer;
	}

	/**
	 * @return the from
	 */
	public String getFrom()
	{
		return from;
	}
}
